package com.mvc4g.example.mockup.client.view.views;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.mvc4g.client.Event;
import com.mvc4g.example.mockup.client.bean.LoginBean;

public class LoginView extends AbstractView {
	
	private TextBox login = null;
	private PasswordTextBox password = null;

	@Override
	protected Widget createWidget() {
		
		Grid grid = new Grid(2,2);
		grid.setText(0, 0, "Login:");
		grid.setText(1, 0, "Password:");
		
		login = new TextBox();
		password = new PasswordTextBox();
		
		grid.setWidget(0, 1, login);
		grid.setWidget(1, 1, password);
		
		VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		mainPanel.add(grid);
		Button button = new Button("Login");
		button.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				LoginBean loginBean = new LoginBean();
				loginBean.setLogin(login.getValue());
				loginBean.setPassword(password.getValue());
				Event e = new Event(LOGIN, loginBean);
				controller.handleEvent(e);				
			}
			
		});
		
		mainPanel.add(button);
		
		return mainPanel;
	}

	@Override
	public void init() {
		//Nothing to do
	}

}
